<template>
  <div class="recommend" ref="recommend">
    <Scroll ref="scroll" class="recommend-content" :data="discList">
      <div>
        <div class="slider-wrapper" v-if="recommends.length">
          <slider>
            <div v-for="(item,index) in recommends" :key="index">
              <a :href="'javascript:void(0);'">
                <img class="needsclick" @load="loadImage" :src="item.imageUrl" />
              </a>
            </div>
          </slider>
        </div>
        <div class="recommend-list">
          <h1 class="list-title">热门歌单推荐</h1>
          <ul>
            <li @click="selectItem(item)" v-for="(item,index) in discList" class="item" :key="index">
              <div class="icon">
                <img v-lazy="item.cover" width="60" height="60" />
              </div>
              <div class="text">
                <h2 class="name">{{item.username}}</h2>
                <p class="desc">{{item.title}}</p>
              </div>
            </li>
          </ul>
        </div>
        <div class="loading-container" v-show="!discList.length">
          <Loading></Loading>
        </div>
      </div>
    </Scroll>
    <router-view></router-view>
  </div>
</template>

<script>
import { getRecommend, getDiscList } from '../../api/recommend'
import Slider from '@/base/slider/slider'
import Scroll from '@/base/scroll/scroll'
import Loading from '@/base/loading/loading.vue'
import { playListMixin } from '@/assets/js/mixin'
import { mapActions, mapMutations } from 'vuex'
export default {
  mixins: [playListMixin],
  data() {
    return {
      recommends: [],
      discList: []
    }
  },
  components: {
    Slider,
    Scroll,
    Loading
  },
  methods: {
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.recommend.style.bottom = bottom
      this.$refs.scroll.refresh()
    },
    _getRecommend() {
      let that = this
      getRecommend().then(
        (res) => {
          console.log(res)
          that.recommends = res.data.banners
        },
        (err) => {
          console.log(err)
        }
      )
    },
    _getDiscList() {
      getDiscList().then(
        (res) => {
          console.log(res)
          res = res.data.data
          if (!res.count || res.count === 0) {
            return 0
          }
          this.discList = res.list
        },
        (err) => {
          console.log(err)
        }
      )
    },
    loadImage() {
      if (!this.checkLoaded) {
        this.$refs.scroll.refresh()
        this.checkLoaded = true
      }
    },
    selectItem(item) {
      this.$router.push({
        path: `/recommend/${item.content_id}`
      })
      this.setDisc(item)
    },
    ...mapMutations({
      setDisc: 'SET_DISC'
    })
  },
  mounted() {
    this._getRecommend()
    this._getDiscList()
  }
}
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~@/assets/stylus/variable';

.recommend {
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;

  .recommend-content {
    height: 100%;
    overflow: hidden;

    .slider-wrapper {
      position: relative;
      width: 100%;
      overflow: hidden;
    }

    .recommend-list {
      .list-title {
        height: 65px;
        line-height: 65px;
        text-align: center;
        font-size: $font-size-medium;
        color: $color-theme;
      }

      .item {
        display: flex;
        box-sizing: border-box;
        align-items: center;
        padding: 0 20px 20px 20px;

        .icon {
          flex: 0 0 60px;
          width: 60px;
          padding-right: 20px;
        }

        .text {
          display: flex;
          flex-direction: column;
          justify-content: center;
          flex: 1;
          line-height: 20px;
          overflow: hidden;
          font-size: $font-size-medium;

          .name {
            margin-bottom: 10px;
            color: $color-text;
          }

          .desc {
            color: $color-text-d;
          }
        }
      }
    }

    .loading-container {
      position: absolute;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}
</style>